<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>添加奖惩</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{../../lib/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{../../css/public.css}" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">部门</label>
        <div class="layui-input-block">
            <select name="department_name" lay-verify="required" lay-filter="empIdFilter">
                <option value="">请选择</option>
                <option th:each="dept:${deptList}" th:value="${dept.id}">
                    <span th:text="${dept.name}"></span>
                </option>
            </select>
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label required">员工</label>
        <div class="layui-input-block">
            <select id="employee_name" name="employee_name" lay-verify="required">
                <option value="">请选择</option>
            </select>
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label required">奖惩主题</label>
        <div class="layui-input-block">
            <input type="text" name="theme" lay-verify="required" lay-reqtext="奖惩主题不能为空" placeholder="请输入奖惩主题" value=""
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">奖惩金额</label>
        <div class="layui-input-block">
            <input type="text" name="price" lay-verify="required" lay-reqtext="奖惩金额不能为空" placeholder="请输入奖惩金额" value=""
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">奖惩类型</label>
        <div class="layui-input-block">
            <select name="status" lay-filter="aihao">
                <option value=""></option>
                <option value="1">奖励</option>
                <option value="0">惩罚</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label required">备注</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入内容" id="message" name="message" class="layui-textarea"></textarea>
        </div>
    </div>



    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">添加奖惩</button>
        </div>
    </div>
</div>
<script th:src="@{../../lib/layui/layui.js}" charset="utf-8"></script>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        //连锁员工
        form.on('select(empIdFilter)', function (data) {
            $('#employee_name').empty();
            var url = "/emp/queryEmpByDept";
            $.post(url, {dept_id: data.value},
                function (data) {
                    $.each(data, function (i, n) {
                        console.log(n.name);
                        $('#employee_name').append(new Option(n.name, n.id));// 下拉菜单里添加元素
                    });
                    layui.form.render("select");//重新渲染 固定写法
                });
        });







        form.on('submit(saveBtn)', function (data) {
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            $("#btn-submit").attr("disabled", "disabled").addClass("layui-btn-disabled");
            //提交数据
            var url = "/reward/add";
            $.post(url, data.field, function (res) {
                console.log(res);
                if (res.code === 0) {
                    //成功提示
                    layer.msg(res.message, {
                        icon: 1,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    }, function () {
                        //关闭窗口,刷新数据表格
                        parent.refreshTable();
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                    });
                } else {
                    $("#btn-submit").removeAttr("disabled", "disabled").removeClass("layui-btn-disabled");
                    //失败则提示错误消息,窗口不关闭
                    layer.msg('新增部门失败,请联系管理员', {
                        icon: 2,
                        time: 2000, //2秒关闭（如果不配置，默认是3秒）
                        anim: 6
                    }, function () {
                        //do something
                    });
                }
            })
            //关掉窗口并刷新
            //失败则提示错误信息
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
    });
</script>
</body>
</html>